package com.phs.main.vo;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings({"rawtypes", "all"})
public class ConvertToVO {

	/**
	 * 将分页结果做成VO
	 *
	 * @param page 结果的分页
	 * @param c    目标VO类
	 * @return VO 分页
	 */
	public static Page<Object> getVOPage(Page page, Class<?> c) {
		List r = new ArrayList();
		page.getRecords().forEach(a -> {
			try {
				Constructor<?> constructor = c.getConstructor(a.getClass());
				r.add(constructor.newInstance(a));
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		page.setRecords(r);
		return page;
	}

	/**
	 * 将结果做成VO
	 *
	 * @param page 结果的分页
	 * @param c    目标VO类
	 * @return VO 分页
	 */
	public static List<Object> getVOList(List<?> list, Class<?> c) {
		List r = new ArrayList();
		list.forEach(a -> {
			try {
				Constructor<?> constructor = c.getConstructor(a.getClass());
				r.add(constructor.newInstance(a));
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		return r;
	}
}
